package aicue1.services;

import aicue1.beans.Customer;
import aicue1.singletons.CustomersSingleton;

import java.util.Date;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

public class CustomerManagementServiceRestImpl implements CustomerManagementServiceRest
{
    @Context
    HttpHeaders httpHeaders;
    
    public Customer getCustomer(String id) throws WebApplicationException
    {
        System.out.println("\n[CustomerManagementService] " + new Date() + "\n\tRetrieving customer with ID " + id);
        System.out.println("[CustomerManagementService] Receiving MIME Type " + httpHeaders.getMediaType());
        System.out.println("[CustomerManagementService] Returning MIME Type " + httpHeaders.getAcceptableMediaTypes().get(0));

        Customer customer = CustomersSingleton.getInstance().getCustomer(id);
        if(customer == null)
        {
            System.out.println("[CustomerManagementService] Success :: false (Customer doesn't exist)");
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        System.out.println("[CustomerManagementService] Success :: true");

        return customer;
    }

    public Response updateCustomer(String id, Customer customer)
    {
        Response response;

        System.out.println("\n[CustomerManagementService] " + new Date() + "\n\tUpdating customer with ID " + id);
        System.out.println("[CustomerManagementService] Receiving MIME Type " + httpHeaders.getMediaType());
        System.out.println("[CustomerManagementService] Returning MIME Type " + httpHeaders.getAcceptableMediaTypes().get(0));

        if(CustomersSingleton.getInstance().updateCustomer(id, customer))
        {
            response =  Response.ok().build();
            System.out.println("[CustomerManagementService] Success :: true");
        }
        else
        {
            response = Response.status(Response.Status.NOT_FOUND).build();
            System.out.println("[CustomerManagementService] Success :: false (Customer doesn't exist)");
        }

        return response;
    }

    public Response addCustomer(Customer customer)
    {
        Response response;

        System.out.println("\n[CustomerManagementService] " + new Date() + "\n\tAdding customer with ID " + customer.getId());
        System.out.println("[CustomerManagementService] Receiving MIME Type " + httpHeaders.getMediaType());
        System.out.println("[CustomerManagementService] Returning MIME Type " + httpHeaders.getAcceptableMediaTypes().get(0));

        if(CustomersSingleton.getInstance().insertCustomer(customer))
        {
            response =  Response.ok().build();
            System.out.println("[CustomerManagementService] Success :: true");
        }
        else
        {
            response = Response.status(Response.Status.CONFLICT).build();
            System.out.println("[CustomerManagementService] Success :: false (Customer already exists)");
        }

        return response;
    }

    public Response deleteCustomer(String id)
    {
        Response response;

        System.out.println("\n[CustomerManagementService] " + new Date() + "\n\tDeleting customer with ID " + id);
        System.out.println("[CustomerManagementService] Receiving MIME Type " + httpHeaders.getMediaType());
        System.out.println("[CustomerManagementService] Returning MIME Type " + httpHeaders.getAcceptableMediaTypes().get(0));

        if(CustomersSingleton.getInstance().deleteCustomer(id))
        {
            response =  Response.ok().build();
            System.out.println("[CustomerManagementService] Success :: true");
        }
        else
        {
            response = Response.status(Response.Status.NOT_FOUND).build();
            System.out.println("[CustomerManagementService] Success :: false (Customer doesn't exist)");
        }

        return response;
    }
}